<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="SPU名称">{{ spu.spuName }}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model="skuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          placeholder="规格描述"
          type="textarea"
          rows="4"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="(attr, index) in attrInfoList"
            :key="attr.id"
          >
            <el-select
              placeholder="请选择"
              value=""
              v-model="attr.attrIdAndValueId"
            >
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="(attrValue, index) in attr.attrValueList"
                :key="attrValue.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="(saleAttr, index) in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select placeholder="请选择" v-model="saleAttr.saleIdAndValueId">
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                v-for="(saleAttrValue, index) in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table border :data="spuImageList" @selection-change="c1">
          <el-table-column
            label="label"
            width="80px"
            type="selection"
          ></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="{ row, $index }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row, $index }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spuImageList: [], //存储图片
      spuSaleAttrList: [], //存储销售属性
      attrInfoList: [], //存储平台属性的数据
      skuInfo: {
        //第一类：收集的数据，父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：通过数据双向绑定v-model收集
        price: 0,
        skuName: "",
        weight: "",
        skuDesc: "",
        //第三类：书写代码自己收集
        skuAttrValueList: [], //平台属性
        skuImageList: [], //图片的字段
        skuSaleAttrValueList: [], //销售属性
        skuDefaultImg: "", //默认图片的地址
      },
      spu: {},
      imglist: [], //收集图片的数据字段，缺失isDefault属性，将来要整理参数
    };
  },
  methods: {
    //收集skuform数据
    async getData(category1Id, category2Id, spu) {
      //收集父组件给予的数据
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.tmId = spu.tmId;
      this.skuInfo.spuId = spu.id;
      this.spu = spu;

      // 获取spu的图片
      let result = await this.$API.spu.reqSpuImageList(spu.id);
      // console.log(result);
      if (result.code == 200) {
        let list = result.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      //获取销售属性
      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id);
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data;
      }
      //获取平台属性的数据
      let result2 = await this.$API.spu.reqAttrList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result2.code == 200) {
        this.attrInfoList = result2.data;
      }
    },
    //table栏表格复选框按钮的事件
    c1(params) {
      //获取到用户选中图片的信息数据，但是缺失isDefault属性
      this.imglist = params;
    },
    //设置默认按钮的排他操作
    changeDefault(row) {
      //遍历为0
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      //点击的为1
      row.isDefault = 1;
      //收集默认图片的地址
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    //取消按钮自定义事件，让父组件通知子组件切换场景0
    cancel() {
      this.$emit("changeScenes", 0);
      //清除数据
      Object.assign(this._data, this.$options.data());
    },
    //保存按钮的回调
    async save() {
      //整理平台属性参数
      //解构
      const { attrInfoList, skuInfo, spuSaleAttrList, imglist } = this;
      //新建数组
      let arr = [];
      attrInfoList.forEach((item) => {
        //有这个属性代表用户进行了选中
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          //携带给服务器的数据应该是对象
          let obj = { attrId, valueId };
          arr.push(obj);
        }
      });
      skuInfo.skuAttrValueList = arr;
      //整理销售属性
      let arr1 = [];
      spuSaleAttrList.forEach((item) => {
        if (item.saleIdAndValueId) {
          const [saleAttrId, saleAttrvalueId] =
            item.saleIdAndValueId.split(":");
          let obj1 = { saleAttrId, saleAttrvalueId };
          arr1.push(obj1);
        }
      });
      skuInfo.skuSaleAttrValueList = arr1;
      //整理图片的数据
      skuInfo.skuImageList = imglist.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
      //最后发请求
      let result = await this.$API.spu.reqAddSku(skuInfo);
      // console.log(result);
      if (result.code == 200) {
        this.$message({ type: "success", message: "sku保存成功" });
        this.$emit("changeScenes", 0);
      }
    },
  },
};
</script>

<style>
</style>